//
//  HZKLineChartView.m
//  Stock_Health
//
//  Created by MayerF on 2017/7/28.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZKLineChartView.h"
#import "MYRadioGroup.h"
#import "MYPortraitRadioGroup.h"
#import "HZKSummaryView.h"
#import "KLineChart.h"
#import "MAModifyView.h"

@interface HZKLineChartView()
@property (nonatomic, strong)UIScrollView  *pScrollView;
@property (nonatomic, strong)UIView        *pConstraintsView; //用来约束scrollview的contensize
@property (nonatomic, strong)HZKSummaryView *pSummaryView;
@property (nonatomic, strong)MYRadioGroup  *pRadioGroup;
@property (nonatomic, strong)MYPortraitRadioGroup *pPortraitRadioGroup;
@property (nonatomic, strong)KLineChart    *pKlineChart;
@property (nonatomic, strong)UIView *pCurrentShowView;
@property (nonatomic, strong)UIButton             *pBackView;
@property (nonatomic, assign)NSInteger nMenuState;

@property (nonatomic, strong)MAModifyView                  *pMaModifyView;//MA值修改的弹出视图
@property (nonatomic, strong)UIPickerView                  *pPickerView;//滚动轮选择框
@property (nonatomic, strong)UIView                        *pPopView;//弹出框
@property (nonatomic, strong)NSArray                       *pPickerData;//滚动轮的数据源
@property (nonatomic, strong)NSString                      *pSelectStr;//pickerView选中项文本
@end

@implementation HZKLineChartView
- (MAModifyView *)pMaModifyView
{
    if (!_pMaModifyView) {
        _pMaModifyView = [[MAModifyView alloc]initWithFrame:CGRectMake((self.frame.size.width - 240)/2, (self.frame.size.height-265)/2, 240, 265)];
        _pMaModifyView.parent = self;
        [self addSubview:_pMaModifyView];
    }
    return _pMaModifyView;
}
- (UIView *)pPopView
{
    if(!_pPopView){
        //弹出框
        _pPopView = [[UIView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height - 260, self.bounds.size.width, 260)];
        UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 44)];
        [pickerDateToolbar sizeToFit];
        pickerDateToolbar.backgroundColor = kMYHexToRGB(0xeaf0f5, 1.0);
        NSMutableArray *barItems = [[NSMutableArray alloc] init];
        UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(toolBarCanelClick)];
        [barItems addObject:cancelBtn];
        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        [barItems addObject:flexSpace];
        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(toolBarDoneClick)];
        [barItems addObject:doneBtn];
        [pickerDateToolbar setItems:barItems animated:YES];
        [_pPopView addSubview:pickerDateToolbar];
        
        _pPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, self.bounds.size.width, 216)];
        _pPickerView.backgroundColor = [UIColor whiteColor];
        _pPickerView.delegate = self;
        _pPickerView.dataSource = self;
        //显示选中框
        _pPickerView.showsSelectionIndicator = YES;
        [_pPopView addSubview:_pPickerView];
        [self addSubview:_pPopView];
        _pPickerData = [[NSArray alloc]initWithObjects:@"MACD",@"KDJ",@"RSI",@"BIAS",
                        @"BOLL",@"CCI",@"WR",@"DMA",@"ARBR",@"PSY", nil];
        
    }
    return _pPopView;
}

- (void)p_loadPortraitRadioGroup
{
    if (!_pPortraitRadioGroup) {
        _pBackView = [UIButton new];
        _pPortraitRadioGroup = [MYPortraitRadioGroup new];
        _pPortraitRadioGroup.parent = self;
        _pPortraitRadioGroup.layer.borderWidth = 0.5;
        _pPortraitRadioGroup.layer.borderColor = [HZCommonTool getThemeSeperateColorWithDefaultColor:nil].CGColor;
        NSArray *titleArray = [NSArray arrayWithObjects:
                               kHZMinute5,
                               kHZMinute15,
                               kHZMinute30,
                               kHZMinute60, nil];
        [_pPortraitRadioGroup setRadioGroup:titleArray];
        _pBackView.backgroundColor = [UIColor clearColor];
        [_pBackView addTarget:self action:@selector(backViewOnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_pBackView];
        [self addSubview:_pPortraitRadioGroup];
        [_pBackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
        }];
        [_pPortraitRadioGroup mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pRadioGroup.mas_bottom);
            make.right.mas_equalTo(self);
            make.height.mas_equalTo(30*4);
            make.width.mas_equalTo(self).multipliedBy(0.2);
        }];
    }
    [self bringSubviewToFront:_pBackView];
    [self bringSubviewToFront:_pPortraitRadioGroup];
    _pBackView.hidden = _pPortraitRadioGroup.hidden = NO;
    _pPortraitRadioGroup.backgroundColor = [HZCommonTool getThemeColor];
//    _pPortraitRadioGroup.backgroundColor = kMYHexToRGB(0xefefef, 1);
}

#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self p_commonInit];
        self.backgroundColor = [HZCommonTool getThemeColor];
    }
    return self;
}
#pragma mark -
#pragma mark private
- (void)p_commonInit
{
    _pScrollView = [UIScrollView new];
    _pConstraintsView = [UIView new];
    _pConstraintsView.backgroundColor = [HZCommonTool getThemeColor];
    _pSummaryView = [HZKSummaryView new];
    _pSummaryView.parent = self;
    _pKlineChart = [KLineChart new];
    _pKlineChart.parent = self;
    _pScrollView.bounces = NO;
//    _pScrollView.delegate = self;
    _pScrollView.canCancelContentTouches = NO;
    [_pKlineChart setMode:KLINE_MODE_SPECIAL];
    [_pKlineChart setCurShowIndex:KLINE_EX_TYPE_MACD];
    
    
    [self addSubview:_pScrollView];
    [_pScrollView addSubview:_pConstraintsView];
    [_pConstraintsView addSubview:_pSummaryView];
    [_pConstraintsView addSubview:_pKlineChart];
    
    _pRadioGroup = [MYRadioGroup new];
    _pRadioGroup.parent = self;
    _pRadioGroup.backgroundColor = [HZCommonTool getThemeColor];
    NSArray *titleArray = [NSArray arrayWithObjects:
                           kHZDayK,
                           kHZWeekK,
                           kHZMonthK,
                           kHZMinute, nil];
    [_pRadioGroup setRadioGroup:titleArray];
    [_pRadioGroup setHiddenSeparateLine:YES andHiddenUnderline:NO];
    [_pRadioGroup setSelectedButton:0];
    [_pRadioGroup resetAllButtonFont:[UIFont systemFontOfSize:15]];
    [self addSubview:_pRadioGroup];
    
    [self p_initConstraints];

    
}
- (void)p_initConstraints
{
    [_pRadioGroup mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(35);
    }];
    [_pScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pRadioGroup.mas_bottom);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
    [_pConstraintsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(_pScrollView);
        make.width.height.mas_equalTo(_pScrollView);
    }];
    [_pSummaryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pConstraintsView);
        make.left.mas_equalTo(_pConstraintsView);
        make.right.mas_equalTo(_pConstraintsView);
        make.height.mas_equalTo(60);
    }];
    [_pKlineChart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pSummaryView.mas_bottom);
        make.left.mas_equalTo(_pConstraintsView);
        make.right.mas_equalTo(_pConstraintsView);
        make.bottom.mas_equalTo(_pConstraintsView).offset(-10);
    }];
    
    [self.superview setNeedsLayout];
    [self.superview layoutIfNeeded];
}

- (void)updateNewData:(HZMinPriceModel *)data
{
    [_pSummaryView updateNewData:data];
    [_pKlineChart updateNewData];
}
- (NSString *)getStateTitle
{
    return [_pRadioGroup getSelectedButton].currentTitle;
}
- (int)getIndexState
{
    return [_pKlineChart getShowIndex];
}
- (void)updateIndexWithState:(int)state
{
    if (state == KLINE_EX_TYPE_VOL) {
        state = KLINE_EX_TYPE_MACD;
    }
    [_pKlineChart setCurShowIndex:state];
}
- (void)updateRadioButtonWithTitle:(NSString *)title
{
    if ([title isEqual:@"日K"]) {
        [_pRadioGroup setSelectedButton:0];
    }else if ([title isEqual:@"周K"]) {
        [_pRadioGroup setSelectedButton:1];
    }else if ([title isEqual:@"月K"]) {
        [_pRadioGroup setSelectedButton:2];
    }else {
        [_pRadioGroup setSelectedButton:3];
        [_pRadioGroup resetSelectedButtonWithTitle:title andIndex:3];
    }
}
#pragma mark -
#pragma mark action
- (void)backViewOnClick
{
    _pBackView.hidden = _pPortraitRadioGroup.hidden = YES;
}

#pragma mark -
#pragma mark Picker Date Source Methods
//返回显示的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
//返回当前列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [_pPickerData count];
}
-(void)toolBarCanelClick{
    _pPopView.hidden = YES;
}
-(void)toolBarDoneClick{
    if ([_pSelectStr isEqualToString:@""]) {
        _pPopView.hidden = YES;
        return;
    }
    NSUInteger index = [_pPickerView selectedRowInComponent:0];
    _pSelectStr = _pPickerData[index];
    
    
    int nIndex = (int)[_pPickerData indexOfObject:_pSelectStr];
    [_pKlineChart setSubChartIndex:nIndex+1];
    
    _pPopView.hidden = YES;
}
#pragma mark Picker Delegate Methods
//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [_pPickerData objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0){
        _pSelectStr = _pPickerData[row];
    }
}
#pragma mark -
- (void)upToParentWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
    switch (type) {
        case kHZ_setLandscapeScreen_Action:
        {
            [self.parent upToParentWithObjc:objc withType:type withParamter:paramter];
        }
            break;
        case kHZ_radioGroupClick_Action:
        {
            if ([paramter isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)paramter;
                _nMenuState = btn.tag;
                if (btn.tag == 3) {
                    [self p_loadPortraitRadioGroup];
                }
                
                //发送日K、周K，月K
                int nKType = -1;
                if ([btn.currentTitle isEqual:kHZDayK]) {
                    nKType = 33;
                }else if ([btn.currentTitle isEqual:kHZWeekK]) {
                    nKType = 39;
                }else if ([btn.currentTitle isEqual:kHZMonthK]) {
                    nKType = 40;
                }
                if (nKType > 0) {
                    [self.parent upToParentWithObjc:self withType:kHZ_ClickKLineRequest_Action withParamter:@(nKType)];
                }
                //通知
//                [[NSNotificationCenter defaultCenter] postNotificationName:kRadioGroupClickNotification object:btn userInfo:nil];
            }
            
        }
            break;
        case kHZ_PortraitRadioGroupClick_Action:
        {
            [self backViewOnClick];
            int nKType = 0;
            if ([paramter isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)paramter;
                nKType += (btn.tag + 35);
                [_pRadioGroup resetSelectedButtonWithTitle:btn.currentTitle andIndex:3];
            }
            [self.parent upToParentWithObjc:self withType:kHZ_ClickKLineRequest_Action withParamter:@(nKType)];
        }
            break;
        case KMSKlineChartViewType:
        {
            if(paramter && [paramter isKindOfClass:[NSString class]]){
                if([paramter isEqualToString:@"Change_Index"]){
                    self.pPopView.hidden = NO;
                    [self.pPickerView reloadAllComponents];
                    return;
                }else if([paramter isEqualToString:@"Change_MA"]){
                    [self.pMaModifyView readData];
                    self.pMaModifyView.hidden = NO;
                    return;
                }else if([paramter isEqualToString:@"Save_MA"]){
                    [_pMaModifyView readData];
                    [_pKlineChart updateNewData];
                    return;
                }
            }
            
        }
            break;
        case KMSClickIsShowTenLineActionType:
        {
            HZMinPriceModel *model = (HZMinPriceModel *)paramter;
            [_pSummaryView updateNewData:model];
        }
            break;
            
        default:
            break;
    }
}
- (void)downToChildWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
}

@end
